// MaxDisplayDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MaxDisplayDlg.h"
//#include ".\maxdisplaydlg.h"


// CMaxDisplayDlg dialog

IMPLEMENT_DYNAMIC(CMaxDisplayDlg, CDialog)
CMaxDisplayDlg::CMaxDisplayDlg(UINT nID, CWnd* pParent /*=NULL*/)
	: CDialog(nID, pParent)
	, m_pCFont(NULL)
	,m_bNeedZoomFont(FALSE)
	,m_bNumKeyDialog(FALSE)
{

}

CMaxDisplayDlg::~CMaxDisplayDlg()
{
	if( NULL != m_pCFont )
	{
		m_pCFont->DeleteObject();
		delete m_pCFont;
		m_pCFont = NULL;
	}
}

BEGIN_MESSAGE_MAP(CMaxDisplayDlg, CDialog)
	ON_WM_SIZE()
	//ON_WM_NCHITTEST()
END_MESSAGE_MAP()


// CMaxDisplayDlg message handlers

BOOL CMaxDisplayDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// TODO:  Add extra initialization here
	SearchAllControl();
    	MaximizeWindow();
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

//Get all Controls and the rect of dialog
void CMaxDisplayDlg::SearchAllControl()
{
	CRect rectWnd;
	GetWindowRect(&rectWnd);
	m_listRect.AddTail(&rectWnd);

	//search child control
	CWnd *pWndChild = GetWindow( GW_CHILD );
	while( pWndChild )
	{
		pWndChild->GetWindowRect( &rectWnd );
		m_listRect.AddTail( &rectWnd );
		pWndChild = pWndChild->GetNextWindow();
	}
}

//Maximize window
void CMaxDisplayDlg::MaximizeWindow()
{
	CRect   rt;   
	int		iScreenWidth;
	int		iScreenHeight;

	iScreenWidth = GetSystemMetrics(SM_CXSCREEN);
	iScreenHeight = GetSystemMetrics(SM_CYSCREEN);

	SystemParametersInfo(SPI_GETWORKAREA, 0, &rt, SPIF_UPDATEINIFILE);  
	switch(iScreenWidth)
	{
	case 640:
	case 800:
	case 1024:
		break;
	default:
		rt.right = 640;
		break;
	}
	switch(iScreenHeight)
	{
	case 480:
	case 600:
	case 768:
		break;
	default:
		rt.bottom = 480;
		break;
	}
	if(m_bNumKeyDialog)
	{
		rt.right = 240;
		rt.bottom = 320;
	}
	MoveWindow(&rt);
}


void CMaxDisplayDlg::OnSize(UINT nType, int cx, int cy)
{
	CDialog::OnSize(nType, cx, cy);

	// TODO: Add your message handler code here

	//Get the Count of current controls
	int iCount = (int)m_listRect.GetCount();
	if( m_listRect.GetCount() > 0 )
	{
		//Get the rect of the maximized dialog
		CRect rectDlgNow;
		GetWindowRect(&rectDlgNow);

		//Get the size of dialog before maximizing
		POSITION mP = m_listRect.GetHeadPosition();
		CRect rectDlgSaved;
		rectDlgSaved = m_listRect.GetNext( mP );

		//Convert to rect of client
		ScreenToClient( rectDlgSaved );
		ScreenToClient( rectDlgNow );

		//Get the ratio 
		float fRateScaleX = (float)( rectDlgNow.right - rectDlgNow.left ) / ( rectDlgSaved.right - rectDlgSaved.left );
		float fRateScaleY = (float)( rectDlgNow.bottom - rectDlgNow.top ) / ( rectDlgSaved.bottom - rectDlgSaved.top );

		//Convert to rect of screen
		ClientToScreen( rectDlgSaved );
		ClientToScreen( rectDlgNow );

		//Compute the size of font
		LOGFONT stFont;
		::GetObject( (HFONT)GetStockObject(DEFAULT_GUI_FONT), sizeof(stFont), &stFont );
		//
		if(m_bNeedZoomFont)
		{
			//_tcscpy_s( stFont.lfFaceName,LF_FACESIZE,FONT_NAME);
			stFont.lfHeight = (LONG)(stFont.lfHeight*fRateScaleY);
			stFont.lfWidth  = (LONG)(stFont.lfWidth*fRateScaleX);
		}
		if(m_pCFont==NULL)
		{
			m_pCFont = new CFont;
			m_pCFont->CreateFontIndirect( &stFont );
		}
		//Compute the size of every control
		CRect rectChildSaved;
		CWnd *pWndChild = GetWindow( GW_CHILD );
		while( pWndChild )
		{
			rectChildSaved = m_listRect.GetNext(mP);
			rectChildSaved.left = rectDlgNow.left+(int)((rectChildSaved.left-rectDlgSaved.left)* fRateScaleX);
			rectChildSaved.top = rectDlgNow.top+(int)((rectChildSaved.top-rectDlgSaved.top)*fRateScaleY);
			rectChildSaved.right = rectDlgNow.right+(int)((rectChildSaved.right-rectDlgSaved.right)* fRateScaleX);
			rectChildSaved.bottom = rectDlgNow.bottom+(int)((rectChildSaved.bottom-rectDlgSaved.bottom)* fRateScaleY);
			ScreenToClient( rectChildSaved );

			TCHAR cClassName[ MAX_PATH ];
			GetClassName( pWndChild->GetSafeHwnd(), LPTSTR(cClassName), MAX_PATH );

			CString strClassName = cClassName;

			CString strWindowText = _T("");
			pWndChild->GetWindowText( strWindowText );

			//elongate the bottom fo the combo box control
			if( strClassName == _T("ComboBox") )
			{
				rectChildSaved.bottom += COMBOBOX_HETGHT;
			}

			pWndChild->MoveWindow( rectChildSaved );
			pWndChild->SetFont( m_pCFont );
			pWndChild = pWndChild->GetNextWindow();		
		}
	}
	Invalidate();
	this->CenterWindow();
}

//Make window can't move
LRESULT CMaxDisplayDlg::OnNcHitTest(CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	LRESULT  nReturn = CDialog::OnNcHitTest(point);   
	if(HTCAPTION == nReturn)   
	{   
		return FALSE;   
	}   
	else   
	{   
		return CDialog::OnNcHitTest(point);   
	}   
}
